package com.xqboss.apps.service.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.consumer.ConsumerTeamMsgRecord;
import com.xqboss.apps.dto.consumer.ConsumerTeamMsgRecordDto;
import com.xqboss.apps.enums.consumer.ConsumerMsgRecordEnum;
import com.xqboss.apps.mapper.consumer.ConsumerTeamMemberMapper;
import com.xqboss.apps.mapper.consumer.ConsumerTeamMsgRecordMapper;
import com.xqboss.apps.mapper.consumer.ConsumerUpvoteRecordMapper;
import com.xqboss.apps.mapper.consumer.ConsumerWarnPunchRecordMapper;
import com.xqboss.apps.mapper.punch.PunchUserRecordMapper;
import com.xqboss.apps.vo.consumer.ConsumerTeamMsgRecordUserVo;
import com.xqboss.apps.vo.consumer.ConsumerTeamMsgRecordVo;
import com.xqboss.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 队伍消息记录Service接口
 *
 * @author xqboss
 * @date 2023-08-18
 */
@Service
public class ConsumerTeamMsgRecordService extends ServiceImpl<ConsumerTeamMsgRecordMapper, ConsumerTeamMsgRecord> {

    @Resource
    private ConsumerTeamMsgRecordMapper consumerTeamMsgRecordMapper;
    @Resource
    private PunchUserRecordMapper punchUserRecordMapper;
    @Resource
    private ConsumerWarnPunchRecordMapper consumerWarnPunchRecordMapper;
    @Resource
    private SysUserMapper sysUserMapper;


    public List<ConsumerTeamMsgRecordVo> msgList(List<ConsumerTeamMsgRecord> msgRecords) {

        List<ConsumerTeamMsgRecordVo> msgRecordVoList = BeanUtil.copyToList(msgRecords, ConsumerTeamMsgRecordVo.class);
        msgRecordVoList.forEach(item -> {
            if (ObjUtil.equal(item.getType(), ConsumerMsgRecordEnum.PUNCH.getValue())) {
                ConsumerTeamMsgRecordUserVo userVo = punchUserRecordMapper.getRecordById(item.getSourceId());
                BeanUtil.copyProperties(userVo, item);

            }
//            else if (ObjUtil.equal(item.getType(), ConsumerMsgRecordEnum.UPVOTE.getValue())) {
//                ConsumerTeamMsgRecordUserVo userVo = consumerUpvoteRecordMapper.getUpvoteRecord(item.getSourceId(), ConsumerMsgRecordEnum.UPVOTE.getValue());
//                item.setContent(userVo.getUserName() + "送给" + userVo.getAcceptName() + ConsumerMsgRecordEnum.UPVOTE.getMsg() + userVo.getLikeCount() + "次~");
//
//            } else if (ObjUtil.equal(item.getType(), ConsumerMsgRecordEnum.SEND_LOVE.getValue())) {
//                ConsumerTeamMsgRecordUserVo userVo = consumerUpvoteRecordMapper.getUpvoteRecord(item.getSourceId(), ConsumerMsgRecordEnum.SEND_LOVE.getValue());
//                item.setContent(userVo.getUserName() + "送给" + userVo.getAcceptName() + userVo.getLikeCount() + "个爱心~");
//
//            }
            else if (ObjUtil.equal(item.getType(), ConsumerMsgRecordEnum.WARN_PUNCH.getValue())) {
                ConsumerTeamMsgRecordUserVo userVo = consumerWarnPunchRecordMapper.getRecordById(item.getSourceId());
                BeanUtil.copyProperties(userVo, item);

            }

        });
        List<ConsumerTeamMsgRecordVo> vo = msgRecordVoList.stream().sorted(Comparator.comparing(ConsumerTeamMsgRecordVo::getId, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        return vo;
    }

    /**
     * 新增队伍消息记录
     * @param teamId 队伍id
     * @param typeEnum 消息类型
     * @param userId 发起人id
     * @param acceptUserId 接收人id
     * @param sourceId 源id
     * @return
     */
    public int insertByType(Integer teamId, ConsumerMsgRecordEnum typeEnum, Long userId, Long acceptUserId, Long sourceId) {

        ConsumerTeamMsgRecord record = new ConsumerTeamMsgRecord();
        record.setTeamId(teamId);
        record.setType(typeEnum.getValue());
        record.setFromUserId(userId);
        record.setToUserId(acceptUserId);
        if (ObjUtil.equal(typeEnum.getValue(), ConsumerMsgRecordEnum.UPVOTE.getValue())) {
            record.setContent(sysUserMapper.getNameById(userId) + "给" + sysUserMapper.getNameById(acceptUserId) + ConsumerMsgRecordEnum.UPVOTE.getMsg() + "1次~");
        } else if (ObjUtil.equal(typeEnum.getValue(), ConsumerMsgRecordEnum.SEND_LOVE.getValue())) {
            record.setContent(sysUserMapper.getNameById(userId) + "送给" + sysUserMapper.getNameById(acceptUserId) + "1个爱心~");
        } else if (ObjUtil.equal(typeEnum.getValue(), ConsumerMsgRecordEnum.WARN_PUNCH.getValue())) {
            record.setContent("提醒" + sysUserMapper.getNameById(acceptUserId) + "打卡~");
        } else if (ObjUtil.equal(typeEnum.getValue(), ConsumerMsgRecordEnum.PUNCH.getValue())) {
            record.setContent("喝水打卡~");
        }
        record.setSourceId(sourceId);
        return consumerTeamMsgRecordMapper.insert(record);
    }

    /**
     * 获取最新的队伍消息记录id
     * @return
     */
    public Integer findLastId() {
        return consumerTeamMsgRecordMapper.findLastId();
    }
}
